
package com.moogene.android.engine.animlite;

import com.moogene.android.InfoMediator;
import com.moogene.android.engine.util.Anchor;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

import java.util.LinkedList;

public class Animlite implements Cloneable {
    private String TAG = "Animlite";

    private FrameAnim frameAnim;

    private RotateAnim rotateAnim;

    private ScaleAnim scaleAnim;

    private AlphaAnim alphaAnim;

    private VibrateAnim vibrateAnim;

    private ColorMatrixAnim colorMatrixAnim;

    DrawableWrapper drawableWrapper;

    LinkedList<Anime> mAnimeList;

    private Anchor anchor;
    
    private int resId;

    public Animlite(int id) {
        this(InfoMediator.getBitmap(id));
        this.resId = id;
    }

    public Animlite(int id, int row, int column) {
        this(InfoMediator.getBitmap(id), row, column);
        this.resId = id;
    }

    public Animlite(Bitmap img) {
        this(img, 1, 1);
    }

    public Animlite(Bitmap img, int row, int column) {
        this(img, row, column, 0, 0);
    }

    public Animlite(Bitmap img, int row, int column, int anchorX, int anchorY) {
        drawableWrapper = new DrawableWrapper();
        frameAnim = new FrameAnim(img, drawableWrapper, row, column);
        mAnimeList = new LinkedList<Anime>();
        mAnimeList.add(frameAnim);
        setAnchor(anchorX, anchorY);
    }

    public void setAnchor(int Type){
        anchor = new Anchor(Type, getFrameWidth(), getFrameHeight());
        drawableWrapper.setDefaultRot(anchor.getAnchorX(), anchor.getAnchorY());
    }
    
    public void setAnchor(int anchorX, int anchorY){
    	anchor = new Anchor(anchorX, anchorY, getFrameWidth(), getFrameHeight());
        drawableWrapper.setDefaultRot(anchorX, anchorY);
    }

    public FrameAnim getFrameAnim() {
        if (frameAnim == null) {
            throw new IllegalStateException("FrameAnim should has been inited");
        }
        return frameAnim;
    }

    public RotateAnim getRotateAnim() {
        if (rotateAnim == null) {
            rotateAnim = new RotateAnim(drawableWrapper);
            mAnimeList.add(rotateAnim);
        }
        return rotateAnim;
    }

    public ScaleAnim getScaleAnim() {
        if (scaleAnim == null) {
            scaleAnim = new ScaleAnim(drawableWrapper);
            mAnimeList.add(scaleAnim);
        }
        return scaleAnim;
    }

    public AlphaAnim getAlphaAnim() {
        if (alphaAnim == null) {
            alphaAnim = new AlphaAnim(drawableWrapper);
            mAnimeList.add(alphaAnim);
        }
        return alphaAnim;
    }

    public VibrateAnim getVibrateAnim() {
        if (vibrateAnim == null) {
            vibrateAnim = new VibrateAnim(drawableWrapper);
            mAnimeList.add(vibrateAnim);
        }
        return vibrateAnim;
    }

    public ColorMatrixAnim getColorMatrixAnim() {
        if (colorMatrixAnim == null) {
            colorMatrixAnim = new ColorMatrixAnim(drawableWrapper);
            mAnimeList.add(colorMatrixAnim);
        }
        return colorMatrixAnim;
    }

    DrawableWrapper getDrawableWrapper() {
        return drawableWrapper;
    }

    public Bitmap getResource() {
        return frameAnim.mImage;
    }

    public int getFrameWidth() {
        return frameAnim.mWidth;
    }

    public int getFrameHeight() {
        return frameAnim.mHeight;
    }

    public int getNumRows() {
        return frameAnim.mRow;
    }

    public int getNumColumns() {
        return frameAnim.mColumn;
    }
    
    public Anchor getAnchor(){
    	return anchor;
    }

    public Rect getDstRect(int left, int top) {
        if (scaleAnim != null) {
            return scaleAnim.getDstRect(left, top, frameAnim.mWidth, frameAnim.mHeight,
                    anchor.getAnchorX(), anchor.getAnchorY());
        }
        return anchor.adjust(left, top);
    }

    public int getResId() {
		return resId;
	}

	void nextFrame() {
        for (Anime a : mAnimeList) {
            a.nextFrame();
        }
    }

    public void draw(Canvas canvas, Rect dest) {
        if (dest == null) {
            Log.d(TAG, "draw(); dest Rect is null");
        }
        drawableWrapper.setDest(dest);
        drawableWrapper.draw(canvas);
        nextFrame();
    }

    public void draw(Canvas canvas, int left, int top) {
        draw(canvas, getDstRect(left, top));
    }
    
	public Animlite copy() {
		Animlite anim = new Animlite(frameAnim.mImage, frameAnim.mRow,
				frameAnim.mColumn, anchor.getAnchorX(), anchor.getAnchorY());
		if(resId != 0){
			anim.resId = resId;
		}
		return anim;
	}
}
